package com.cipe.cmrs.model.view;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.TemporalType;
import javax.persistence.Temporal;
import javax.persistence.Transient;

import com.cipe.cmrs.model.BaseEntity;
import com.cipe.cmrs.util.DateUtil2;
import com.cipe.cmrs.util.StringUtils;
import com.cipe.cmrs.util.Util;

@Entity
@Table(name = "vw_projectpayment")
public class ProjectPaymentView extends BaseEntity {

	/**
     * 
     */
	private static final long serialVersionUID = 3676012836039686896L;

	@Column(name = "projectid")
	private String projectId;

	@Column(name = "title")
	private String title;

	@Column(name = "costcenternumber")
	private String costCenterNumber;

	@Column(name = "projecttypedesc")
	private String projectTypeDesc;

	@Column(name = "status")
	private String status;

	@Column(name = "region")
	private String region;

	@Column(name = "country")
	private String country;

	@Column(name = "acronym")
	private String acronym;

	@Column(name = "projectstartdate")
	@Temporal(TemporalType.TIMESTAMP)
	private Date projectStartDate;

	@Column(name = "projectenddate")
	@Temporal(TemporalType.TIMESTAMP)
	private Date projectEndDate;

	@Column(name = "partnername")
	private String partnerName;

	@Column(name = "budgetoverride")
	private Boolean budgetOverride;

	@Column(name = "statusoverride")
	private Boolean statusOverride;

	@Column(name = "totalfunds")
	private BigDecimal totalFunds;

	@Column(name = "remainingfunds")
	private BigDecimal remainingFunds;

	@Transient
	private BigDecimal remainingFundsPRExcluded;

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getCostCenterNumber() {
		return costCenterNumber;
	}

	public void setCostCenterNumber(String costCenterNumber) {
		this.costCenterNumber = costCenterNumber;
	}

	public String getProjectTypeDesc() {
		return projectTypeDesc;
	}

	public void setProjectTypeDesc(String projectTypeDesc) {
		this.projectTypeDesc = projectTypeDesc;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getAcronym() {
		return acronym;
	}

	public void setAcronym(String acronym) {
		this.acronym = acronym;
	}

	public Date getProjectStartDate() {
		return projectStartDate;
	}

	public void setProjectStartDate(Date projectStartDate) {
		this.projectStartDate = projectStartDate;
	}

	public Date getProjectEndDate() {
		return projectEndDate;
	}

	public void setProjectEndDate(Date projectEndDate) {
		this.projectEndDate = projectEndDate;
	}

	public String getPartnerName() {
		return partnerName;
	}

	public void setPartnerName(String partnerName) {
		this.partnerName = partnerName;
	}

	public Boolean getBudgetOverride() {
		return budgetOverride;
	}

	public void setBudgetOverride(Boolean budgetOverride) {
		this.budgetOverride = budgetOverride;
	}

	public Boolean getStatusOverride() {
		return statusOverride;
	}

	public void setStatusOverride(Boolean statusOverride) {
		this.statusOverride = statusOverride;
	}

	public BigDecimal getTotalFunds() {
		return totalFunds;
	}

	public void setTotalFunds(BigDecimal totalFunds) {
		this.totalFunds = totalFunds;
	}

	public BigDecimal getRemainingFunds() {
		return remainingFunds;
	}

	public void setRemainingFunds(BigDecimal remainingFunds) {
		this.remainingFunds = remainingFunds;
	}

	public BigDecimal getRemainingFundsPRExcluded() {
		return remainingFundsPRExcluded;
	}

	public void setRemainingFundsPRExcluded(BigDecimal remainingFundsPRExcluded) {
		this.remainingFundsPRExcluded = remainingFundsPRExcluded;
	}

	@Transient
	public String getEscapedTitle() {
		return StringUtils.INSTANCE.escape(title);
	}

	@Transient
	public String getEscapedRegion() {
		return StringUtils.INSTANCE.escape(region);
	}

	@Transient
	public String getEscapedCountry() {
		return StringUtils.INSTANCE.escape(country);
	}

	@Transient
	public String getEscapedAcronym() {
		return StringUtils.INSTANCE.escape(acronym);
	}

	@Transient
	public String getFormattedProjectStartDate() {
		return DateUtil2.INSTANCE.formatDate(projectStartDate);
	}

	@Transient
	public String getFormattedProjectEndDate() {
		return DateUtil2.INSTANCE.formatDate(projectEndDate);
	}

	@Transient
	public String getEscapedPartnerName() {
		return StringUtils.INSTANCE.escape(partnerName);
	}

	@Transient
	public String getDisplayName() {
		String result = "";
		String outcome = Util
				.concate(
						new String[] {
								getValue(projectId),
								getValue(getEscapedAcronym()),
								getValue(projectTypeDesc),
								getValue(status),
								getValue(getEscapedPartnerName()),
								getValue(getEscapedTitle()),
								getValue(getFormattedProjectStartDate()),
								getValue(getFormattedProjectEndDate()),
								getValue(this.formatDecimal(remainingFundsPRExcluded)), },
						" | ");
		if (outcome.length() > 200) {
			result = outcome.substring(0, 199);
			if (result.endsWith("\\")) {
				result = outcome.substring(0, 200);
			}
		} else {
			result = outcome;
		}

		return result;
	}

	public static void main(String[] args) {
		String s = StringUtils.INSTANCE.escape("Moldova's");
		String s1 = s.substring(0, "Moldova".length() + 1);
		System.out.println(s1);
		if (s1.endsWith("\\")) {
			System.out.println("test");
		}
	}

	private String getValue(String val) {
		if (org.apache.commons.lang.StringUtils.isNotBlank(val)) {
			return val;
		} else {
			return "";
		}
	}

	public String formatDecimal(BigDecimal b) {
		NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
		double doublePayment = b.doubleValue();
		String s;
		if (doublePayment < 0) {
			s = n.format(doublePayment * -1);
			s = "-" + s;
		} else {
			s = n.format(doublePayment);
		}
		return s;
	}
}
